//
//  ZYXMineCenterViewController.m
//  RuiBeiKang
//
//  Created by 代码女神 on 2019/1/7.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXMineCenterViewController.h"
#import "ZYXChangeNameViewController.h"
#import "ZYXPhotoView.h"

@interface ZYXMineCenterViewController ()<ZYXPhotoViewDelegate, ZYXChangeNameViewControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) ZYXPhotoView *photoView;

@end

@implementation ZYXMineCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)setupViews{
    self.navigationItem.title = @"个人中心";
    [self.view addSubview:self.photoView];
}
-(void)loadData{

    [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_WDUserInfo) parmas:@{@"user_id":UID} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            UserModel *model = [UserModel mj_objectWithKeyValues:responseObject[@"data"]];
            self.nameLabel.text = model.nickname;
            ///图片地址
            [self.headImgView sd_setImageWithURL:[NSURL URLWithString:kImgAllUrl(model.avatar)] placeholderImage:IMG(@"touxiang")];
            
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
        
    } failureBlock:^(NSError *error) {
        
    }];
    
}
#pragma mark Action
- (IBAction)headAction:(id)sender {
    self.photoView.hidden = NO;
}
- (IBAction)nameAction:(id)sender {
    ZYXChangeNameViewController *ctrl = [[ZYXChangeNameViewController alloc] init];
    ctrl.delegate = self;
    [self.navigationController pushViewController:ctrl animated:YES];
}
#pragma mark ZYXPhotoViewDelegate
/** 点击拍照 */
- (void)didClickTakePhoto{
    NSLog(@"拍照");
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        //设置选择后的图片可被编辑
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        [JKToast showWithText:@"请在真机中打开"];
    }
}
/** 点击相册 */
- (void)didClickAlbum{
    NSLog(@"相册");
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    //设置拍照后的图片可被编辑
    picker.allowsEditing = YES;
    picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"1111");
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
      self.headImgView.image = newPhoto;
//        [picker dismissViewControllerAnimated:YES completion:nil];
    [NetWork uploadUrl:kInterfaceAllUrl(PATH_WDUserImgUpdate) img:@[newPhoto] pramas:@{@"user_id":UID} successBloock:^(NSDictionary  *responseObject) {
        NSLog(@"%@",responseObject);

        [picker dismissViewControllerAnimated:YES completion:nil];
    } failureBlock:^(NSError *error) {

    }];
}
#pragma mark ZYXChangeNameViewControllerDelegate
- (void)didSaveUserName:(NSString *)name{
    if (name.length>0) {
        self.nameLabel.text = name;
    }
}

#pragma mark 懒加载
- (ZYXPhotoView *)photoView{
    if (!_photoView){
        _photoView = [[NSBundle mainBundle] loadNibNamed:@"ZYXPhotoView" owner:nil options:nil].lastObject;
        _photoView.frame = self.view.bounds;
        _photoView.delegate = self;
        _photoView.hidden = YES;
    }
    return _photoView;
}

@end
